#!/bin/bash

# VMware Fusion 虚拟机管理脚本
VMWARE_PATH="/Applications/VMware Fusion.app/Contents/Public"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 显示帮助
show_help() {
    echo -e "${BLUE}VMware Fusion 虚拟机管理脚本${NC}"
    echo "用法: $0 [选项] [虚拟机名称]"
    echo ""
    echo "选项:"
    echo "  list                   显示所有虚拟机"
    echo "  start <虚拟机名称>     启动指定虚拟机"
    echo "  stop <虚拟机名称>      关闭指定虚拟机"
    echo "  suspend <虚拟机名称>   暂停指定虚拟机"
    echo "  resume <虚拟机名称>    恢复指定虚拟机"
    echo "  status <虚拟机名称>    显示指定虚拟机状态"
    echo "  help                   显示此帮助信息"
}

# 检查安装
check_vmware() {
    if [ ! -f "$VMWARE_PATH/vmrun" ]; then
        echo -e "${RED}错误: VMware Fusion 未安装或 vmrun 工具未找到${NC}"
        exit 1
    fi
}

# 列出虚拟机
list_vms() {
    echo -e "${BLUE}正在获取虚拟机列表...${NC}"
    echo ""
    
    # 获取正在运行的虚拟机
    local running_vms
    running_vms=$("$VMWARE_PATH/vmrun" -T fusion list 2>/dev/null)
    
    # 查找所有虚拟机文件
    local all_vms
    all_vms=$(find ~/Virtual\ Machines.localized -name "*.vmwarevm" -type d 2>/dev/null)
    
    if [ -z "$all_vms" ]; then
        echo -e "${YELLOW}未找到任何虚拟机${NC}"
        return
    fi
    
    echo -e "${GREEN}所有虚拟机列表:${NC}"
    echo "----------------------------------------"
    
    local count=1
    while IFS= read -r vm_path; do
        if [ -n "$vm_path" ]; then
            # 提取虚拟机名称
            local vm_name=$(basename "$vm_path" .vmwarevm)
            
            # 检查是否正在运行
            if echo "$running_vms" | grep -q "$vm_path"; then
                echo "$count. $vm_name"
                echo -e "   状态: ${GREEN}运行中${NC}"
                echo "   路径: $vm_path"
            else
                echo "$count. $vm_name"
                echo -e "   状态: ${YELLOW}已停止${NC}"
                echo "   路径: $vm_path"
            fi
            echo ""
            ((count++))
        fi
    done <<< "$all_vms"
    
    echo "----------------------------------------"
    echo "总计: $((count-1)) 个虚拟机"
    
    # 显示运行统计
    if [ -n "$running_vms" ]; then
        local running_count=$(echo "$running_vms" | grep -c "\.vmwarevm")
        echo -e "运行中: ${GREEN}$running_count${NC} 个"
    else
        echo -e "运行中: ${YELLOW}0${NC} 个"
    fi
}

# 启动虚拟机
start_vm() {
    local vm_name="$1"
    if [ -z "$vm_name" ]; then
        echo -e "${RED}错误: 请指定虚拟机名称${NC}"
        exit 1
    fi
    
    echo -e "${BLUE}正在启动虚拟机: $vm_name${NC}"
    local vm_path=$(find ~/Virtual\ Machines.localized -name "*.vmwarevm" -type d 2>/dev/null | grep -i "$vm_name" | head -1)
    
    if [ -z "$vm_path" ]; then
        echo -e "${RED}未找到虚拟机: $vm_name${NC}"
        exit 1
    fi
    
    "$VMWARE_PATH/vmrun" -T fusion start "$vm_path"
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}虚拟机启动成功${NC}"
    else
        echo -e "${RED}虚拟机启动失败${NC}"
    fi
}

# 关闭虚拟机
stop_vm() {
    local vm_name="$1"
    if [ -z "$vm_name" ]; then
        echo -e "${RED}错误: 请指定虚拟机名称${NC}"
        exit 1
    fi
    
    echo -e "${BLUE}正在关闭虚拟机: $vm_name${NC}"
    local vm_path=$(find ~/Virtual\ Machines.localized -name "*.vmwarevm" -type d 2>/dev/null | grep -i "$vm_name" | head -1)
    
    if [ -z "$vm_path" ]; then
        echo -e "${RED}未找到虚拟机: $vm_name${NC}"
        exit 1
    fi
    
    "$VMWARE_PATH/vmrun" -T fusion stop "$vm_path" hard
    echo -e "${GREEN}虚拟机已关闭${NC}"
}

# 暂停虚拟机
suspend_vm() {
    local vm_name="$1"
    if [ -z "$vm_name" ]; then
        echo -e "${RED}错误: 请指定虚拟机名称${NC}"
        exit 1
    fi
    
    echo -e "${BLUE}正在暂停虚拟机: $vm_name${NC}"
    local vm_path=$(find ~/Virtual\ Machines.localized -name "*.vmwarevm" -type d 2>/dev/null | grep -i "$vm_name" | head -1)
    
    if [ -z "$vm_path" ]; then
        echo -e "${RED}未找到虚拟机: $vm_name${NC}"
        exit 1
    fi
    
    # 检查虚拟机是否正在运行
    local running_vms
    running_vms=$("$VMWARE_PATH/vmrun" -T fusion list)
    
    if echo "$running_vms" | grep -q "$vm_path"; then
        echo "虚拟机正在运行，正在暂停..."
        "$VMWARE_PATH/vmrun" -T fusion suspend "$vm_path"
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}虚拟机 '$vm_name' 已暂停${NC}"
        else
            echo -e "${RED}虚拟机暂停失败${NC}"
            exit 1
        fi
    else
        echo -e "${YELLOW}虚拟机 '$vm_name' 未在运行，无法暂停${NC}"
        exit 1
    fi
}

# 恢复虚拟机
resume_vm() {
    local vm_name="$1"
    if [ -z "$vm_name" ]; then
        echo -e "${RED}错误: 请指定虚拟机名称${NC}"
        exit 1
    fi
    
    echo -e "${BLUE}正在恢复虚拟机: $vm_name${NC}"
    local vm_path=$(find ~/Virtual\ Machines.localized -name "*.vmwarevm" -type d 2>/dev/null | grep -i "$vm_name" | head -1)
    
    if [ -z "$vm_path" ]; then
        echo -e "${RED}未找到虚拟机: $vm_name${NC}"
        exit 1
    fi
    
    # 检查虚拟机是否正在运行
    local running_vms
    running_vms=$("$VMWARE_PATH/vmrun" -T fusion list)
    
    if echo "$running_vms" | grep -q "$vm_path"; then
        echo -e "${YELLOW}虚拟机 '$vm_name' 已在运行，无需恢复${NC}"
        exit 1
    else
        echo "虚拟机未运行，正在恢复..."
        "$VMWARE_PATH/vmrun" -T fusion start "$vm_path"
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}虚拟机 '$vm_name' 恢复成功${NC}"
        else
            echo -e "${RED}虚拟机恢复失败${NC}"
            exit 1
        fi
    fi
}

# 显示状态
show_status() {
    local vm_name="$1"
    if [ -z "$vm_name" ]; then
        echo -e "${RED}错误: 请指定虚拟机名称${NC}"
        exit 1
    fi
    
    echo -e "${BLUE}虚拟机状态: $vm_name${NC}"
    local vm_path=$(find ~/Virtual\ Machines.localized -name "*.vmwarevm" -type d 2>/dev/null | grep -i "$vm_name" | head -1)
    
    if [ -z "$vm_path" ]; then
        echo -e "${RED}未找到虚拟机${NC}"
        exit 1
    fi
    
    local running_vms=$("$VMWARE_PATH/vmrun" -T fusion list)
    if echo "$running_vms" | grep -q "$vm_path"; then
        echo -e "状态: ${GREEN}运行中${NC}"
    else
        echo -e "状态: ${YELLOW}已停止${NC}"
    fi
}

# 主函数
main() {
    check_vmware
    
    case "$1" in
        "list")
            list_vms
            ;;
        "start")
            start_vm "$2"
            ;;
        "stop")
            stop_vm "$2"
            ;;
        "suspend")
            suspend_vm "$2"
            ;;
        "resume")
            resume_vm "$2"
            ;;
        "status")
            show_status "$2"
            ;;
        "help"|"-h"|"--help")
            show_help
            ;;
        *)
            show_help
            ;;
    esac
}

main "$@"
